<?php
/**
 * 问题处理进度
 */
class Application_Model_ProblemWay extends Application_Model_DomainObject {

    protected $_id;
    protected $_wayId;
    protected $_status;
    protected $_schedule;
    protected $_type;
    /**
     * 待客户回复
     */
    const Problem_StayUser = 1;
    /**
     * 客户已回复
     */
    const Problem_StayUuch = 2;
    /**
     * 部分退回
     */
    const Problem_PartReturn = 11;
    /**
     * 全部退回
     */
    const Problem_ReturnCancel = 12;
    /**
     * 取消
     */
    const Problem_Cancel = -1;
    /**
     * 放行
     */
    const Problem_Allclear = 100;
    /**
     * 初始化
     */
    const Schedule_Default = 0;
    /**
     * 申请退回
     */
    const Schedule_ApplyFor = 1;
    /**
     * 退回途中
     */
    const Schedule_ReturnRoute = 2;
    /**
     * 已退回渠道商
     */
    const Schedule_InChannel = 3;
    /**
     * 已退回悠奇
     */
    const Schedule_InUuch = 4;
    /**
     * 已入库
     */
    const Schedule_InStore = 5;
    /**
     * 申请退包类型
     */
    const Schedule_Reason = 200;
    /**
     * 国际运单
     */
    const Type_Bill = 1;
    /**
     * 上门取件
     */
    const Type_Visiting =2;

    public function getStatusName()
    {
        switch ($this->_status) {
            case Application_Model_ProblemWay::Problem_StayUser:
                return "等待客户回复";
                break;
            case Application_Model_ProblemWay::Problem_StayUuch:
                return "客户已回复";
                break;
            case Application_Model_ProblemWay::Problem_PartReturn:
                return "部分退回";
                break;
            case Application_Model_ProblemWay::Problem_ReturnCancel:
                return "退件";
                break;
            case Application_Model_ProblemWay::Problem_Allclear:
                return "放行";
                break;
            default:
                return "未知状态";
                break;
        }
    }


    public static function getProblemScheduleByCount($problem = null, $schedule = null ,$type = Application_Model_ProblemWay::Type_Bill) {
        if ($problem != null) {
            $where[" status = "] = $problem;
            $and = " and ";
        }
        if ($schedule != null) {
            $where[$and . " schedule = "] = $schedule;
            $and = " and ";
        }else{
            $where[$and . " schedule is null and "] = 1;
            $and = " and ";
        }
        $where[$and . " type =  "] = $type;
        $and = " and ";
        return self::getDao()->count($where);
    }

    public static function getBillAll($balusc, $agentid, $channelid, $starttime, $endtime, $statusid, $problem, $pagesize, $pageindex, $invoice, $cityid, $startweight, $endweight, $startpayment, $endpayment, $startmoney, $endmoney, $isAddform, $isinvoice,$stus,$schedule)
   {
        $where = array();
        $and = '';
        $or = '';
        if ($balusc != '') {
            $balusc = trim($balusc);
            $num = strlen($balusc);
            $where["( "] = 1;
            $or = " and ";
            if ($num == 5) {
                $uwhere[' storeno = '] = $balusc;
                $user = Application_Model_User::getDao()->queryOne($uwhere);
                if ($user) {
                    $where[$or . ' userId = '] = $user->id;
                    $or = " or ";
                } else {
                    $where[$or . ' userId = '] = "";
                    $or = " or ";
                }
            } else if ($num == 11) {
                $storeItem = Application_Model_StoreItem::bybarcode($balusc);
                $wayid = "(";
                if ($storeItem) {
                    $wayItem = $storeItem->getStoreIdWay();
                    if ($wayItem) {
                        foreach ($wayItem as $wayval) {
                            $wayid .= $wayval->wayId . ",";
                        }
                    }
                }
                $wayid .= "0)";
                $where[$or . " id in "] = $wayid;
                $or = " or ";
            } else {
                $where[$or . ' billNo = '] = $balusc;
                $or = " or ";
                $where[$or . ' agentNo = '] = $balusc;
                $where[$or . ' logistics = '] = $balusc;
            }
            $where[") and "] = "1=1";
            $and = ' and ';
            $problem = 0;
        }

        if ($starttime != '') {
            $where[$and . 'datePaid >='] = $starttime . " 00:00:00";
            $and = ' and ';
        }
        if ($endtime != '') {
            $where[$and . 'datePaid <='] = $endtime . " 23:59:59";
            $and = ' and ';
        }

        if ($agentid != '') {
            $where[$and . ' agentId = '] = $agentid;
            $and = ' and ';
        }
        if ($channelid != '') {
            $where[$and . ' expressId = '] = $channelid;
            $and = ' and ';
        }
        if (!is_null($statusid) && $statusid != "") {
            $where[$and . " interior = "] = $statusid;
            $and = " and ";
        }

        if ($invoice != 0) {
            $where[$and . ' isInvoice = '] = 0;
            $and = ' and ';
        }
        if ($isinvoice != 0) {
            $where[$and . ' isInvoice = '] = 0;
            $and = ' and ';
            $where[$and . " interior < "] = Application_Model_wayBillItem::Interior_Outstorage;
            $where[$and . " interior != "] = Application_Model_wayBillItem::Interior_Cancel;
        }
        if ($isAddform != 0) {
            $where[$and . ' isAddform = '] = 0;
            $and = ' and ';
        }
        if ($cityid != null) {
            $where[$and . " state = "] = $cityid;
            $and = " and ";
        }
        if ($startweight != null && $endweight != null) {
            $where[$and . " productWeight >= "] = $startweight * 1000;
            $and = " and ";
            $where[$and . " productWeight <= "] = $endweight * 1000;
        }
        if ($startpayment != null && $endpayment != null) {
            $where[$and . " paymentWeight >= "] = $startpayment * 1000;
            $and = " and ";
            $where[$and . " paymentWeight <= "] = $endpayment * 1000;
        }
        if ($startmoney != null && $endmoney != null) {

            $where[$and . " shipMoney >= "] = $startmoney;
            $and = " and ";
            $where[$and . " shipMoney <= "] = $endmoney;
        }
        $result = null;
        if($where != array()){
            $result = Application_Model_wayBillItem::getDao()->queryAll($where, " datePaid desc ");
        }
        $waywhere = array();
        $wayand = "";
        if ($result != null) {
            $wayId = "(";
            foreach ($result as $wayVal) {
                $wayId .= $wayVal->id . ",";
            }
            $wayId .= "0)";
            $waywhere[" wayId in "] = $wayId;
            $wayand = " and ";
        }
        if ($stus != null)
        {
            $waywhere[$wayand." status = "] = $stus;
            $wayand = " and ";
        }
        if ($schedule != null)
        {
            $waywhere[$wayand." schedule = "] = $schedule;
            $wayand = " and ";
        }else{
            $where[$and . " schedule is null and "] = 1;
            $and = " and ";
        }
        return self::getDao()->queryPage($waywhere, null, $pagesize, $pageindex);
    }

    public static function getByTypeWayId($wayId,$type = Application_Model_ProblemWay::Type_Bill) {
        return self::getDao()->queryOne(array('wayId = '=>$wayId," and type = "=>$type));
    }

    public static function addProblemWay($billid,$status, $schedule) {
        $problemItem = self::getWayId($billid);
        if (!$problemItem) {
            $problemItem = new Application_Model_ProblemWay();
        }
        $problemItem->wayId = $billid;
        $problemItem->status = $status;
        $problemItem->schedule = $schedule;
        $problemItem->save();
    }
    
    private $_waybill;
    public function getWay()
    {
        if(!$this->_waybill){
            $this->_waybill = Application_Model_wayBillItem::load($this->_wayId);
        }
        return $this->_waybill;
    }
    private $_scheduleDetaile;
    public function getReasonBySchedule()
    {
        if(!$this->_scheduleDetaile){
            $where[" wayId = "] = $this->_wayId;
            $where[" and schedule = "] = Application_Model_ProblemWay::Schedule_Reason;
            $this->_scheduleDetaile = Application_Model_ProblemSchedule::getDao()->queryOne($where);
        }
        return $this->_scheduleDetaile;
        
    }
}